package com.mvc.business.service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.base.model.Picture;
import com.mvc.base.service.BaseCompanyManager;
import com.mvc.base.service.CustomerManager;
import com.mvc.base.service.PictureManager;
import com.mvc.business.model.Order;
import com.mvc.business.model.Quotation;
import com.mvc.business.model.QuotationItem;
import com.mvc.framework.dao.HibernateIdUtils;

@Service
public class QuotationManager extends BaseCompanyManager<Quotation, Integer> {

	@Autowired
	private QuotationItemManager	  quotationItemManager;

	@Autowired
	private QuotationExtraItemManager	quotationExtraItemManager;

	@Autowired
	private PictureManager	          pictureManager;

	@Autowired
	private CustomerManager	          customerManager;

	public Quotation get(Serializable id) {
		Quotation order = super.get(id);
		if (order != null) {
			order.setCustomer(customerManager.getByObjectId(order.getCustomerId()));
			order.setCompany(companyManager.getByObjectId(order.getCompanyId()));
			order.setPictures(pictureManager.getPicturesByItemIdAndType(order.getObjectId(), Picture.PICTURE_OF_QUOTATION));
		}
		return order;
	}

	public Quotation getObject(Integer packingId) {
		Quotation packing = getByObjectId(packingId);
		packing.setCustomer(customerManager.getByObjectId(packing.getCustomerId()));
		packing.setCompany(companyManager.getByObjectId(packing.getCompanyId()));
		packing.setQuotationItems(quotationItemManager.getQuotationItemByQuotationId(packing.getObjectId()));
		packing.setPictures(pictureManager.getPicturesByItemIdAndType(packing.getObjectId(), Picture.PICTURE_OF_QUOTATION));
		packing.setQuotationExtraItems(quotationExtraItemManager.getQuotationExtraItemByQuotationId(packing.getObjectId(), null));
		return packing;
	}

	public void save(Quotation entity) {
		calculate(entity);
		if (null == entity.getId()) {
			entity.setCompanyId(customerManager.getByObjectId(entity.getCustomerId()).getCompanyId());
			entity.setStatus(Order.ORDER_STATUS_DRAFT);
			entity.setGenerateDate(new Date());
			Integer oldOrderId = entity.getObjectId();
			entity.setObjectId(HibernateIdUtils.getUnCacheCustomizeId(getHibernateTemplate().getSessionFactory(), "quotationNo"));
			quotationItemManager.updateQuotationId(oldOrderId, entity.getObjectId());
			quotationExtraItemManager.updateQuotationId(oldOrderId, entity.getObjectId());
			pictureManager.updateQuotationId(oldOrderId, entity.getObjectId());
		}
		super.save(entity);
	}

	public void updateTotal(Integer orderId) {
		Quotation order = getByObjectId(orderId);
		if (order != null) {
			calculate(order);
			super.save(order);
		}
	}

	protected void calculate(Quotation order) {
		if (order != null) {
			double total = 0D;
			double quantity = 0D;
			List<QuotationItem> orderItems = quotationItemManager.getQuotationItemByQuotationId(order.getObjectId());
			for (QuotationItem orderItem : orderItems) {
				total += (orderItem.getTitle() * orderItem.getQuantity() * orderItem.getUnitPrice());
				quantity += (orderItem.getTitle() * orderItem.getQuantity());
			}
			order.setQuantity(quantity);
			order.setTotal(total);
		}
	}
}
